﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sinacor.Infra.UI.Session.Infra.KeepAlive.KeepAlive;
using Sinacor.Infra.UI.Security.Services;
using Sinacor.Infra.UI.Common;
using PSE.Framework.Common.Security.Services.Configuration;

namespace Sinacor.Infra.UI.Session
{
    public class ServerKeepAliveCommunication : IServerKeepAlive
    {
        #region IServerKeepAlive Members

        public void SendServerKeepAlive()
        {
            IKeepAlive service = null;

            try
            {
                Session session = SessionManager.GetInstance().GetCurrentSession();

                if (session != null)
                {
                    SessionInformation sessionInfo = session.GenerateSessionInformation();

                    using (ServiceInstance<IKeepAlive> si = new ServiceInstance<IKeepAlive>())
                    {
                        service = si.TryGetInstance(sessionInfo,CriptographyLevelType.Basic);
                        service.KeepCommunicationAlive();
                    }
                }
            }
            catch (Exception ex)
            {
                Sinacor.Infra.UI.ErrorLogging.ErrorLogger.WriteLog(PSE.Framework.ErrorLogging.LogType.Error, new Exception("Error sending keep alive", ex));

                //Tratamento de Logging
                //string user = "";
                //int companyId = 0;

                //Session session = SessionManager.GetInstance().GetCurrentSession();

                //if (session != null)
                //{
                //    user = session.User.LoginName;
                //    companyId = session.Company.Id;
                //}

                //Sinacor.Infra.Common.Logging.Logger.Write(user, Sinacor.Infra.Common.Logging.LogType.Error, "", companyId, "Infra", "SendServerKeepAlive", ex, "", "Server Keep Alive Communication Error", Sinacor.Infra.Common.Logging.eErrorType.ErroGeral);
            }
        }

        #endregion
    }
}
